

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n, k, om, m, u, j, i, kms: integer;
  arr, mass: array of integer;
  fin, fout: textfile;
  
procedure Heapify (num: integer);
var a, b, s: integer;
begin
  while(1=1) do
  begin
    a:=-2000000000;
    b:=-2000000000;
    if (2*num+1<m) then
      a:=arr[2*num+1];
    if (2*num+2<m) then
      b:=arr[2*num+2];
    if (a>=b) and (a>arr[num]) then
    begin
      s:=arr[2*num+1];
      arr[2*num+1]:=arr[num];
      arr[num]:=s;
      num:=2*num+1;
    end else if (b>=a) and (b>arr[num]) then
    begin
      s:=arr[2*num+2];
      arr[2*num+2]:=arr[num];
      arr[num]:=s;
      num:=2*num+2;
    end else
      break;
  end;
end;

procedure SurfUp(su: integer);
var
  t:integer;
begin
  while (arr[(su-1) div 2]<arr[su]) do
  begin
    t:=arr[su];
    arr[su]:=arr[(su-1) div 2];
    arr[(su-1) div 2]:=t;
    su:=(su-1) div 2;
  end;
end;

begin
  assignfile(fin, 'input.txt');
  reset(fin);
  assignfile(fout, 'output.txt');
  rewrite(fout);
  kms:=0;
  readln(fin, n);
  m:=0;
  for i:=0 to n-1 do
  begin
    read(fin, k);
    if (k=0) then
    begin
      m:=m+1;
      readln(fin, om);
      setlength(arr, m);
      arr[m-1]:=om;
      if (m<>1) then
        SurfUp(m-1);
    end else begin
      kms:=kms+1;
      setlength(mass, kms);
      mass[kms-1]:=arr[0];
      if (m<>1) then
      begin
        u:=arr[0];
        arr[0]:=arr[m-1];
        arr[m-1]:=u;
        m:=m-1;
        if (m<>1) then
          for j:=m-1 downto 0 do
            Heapify(j);
      end;
    end;
  end;
  for i:=0 to kms-1 do
    writeln(fout, mass[i]);
  closefile(fin);
  closefile(fout);
end.
